package com.decent.ejfadmin.user.dao.provider;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.RecommendUser;
import com.decent.ejfadmin.utils.SQLWrapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 推荐返利用户列表
 * @author weicx
 * @date 2018/07/28
 */
public class RecommendUserDaoProvider {
    /**
     * 查询推荐返利用户列表数量
     * @param param 参数
     * @return
     */
    public String queryRecommendUserCount(HashMap<String, Object> param) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("SELECT COUNT(1) ");
        generateSupplyOrderSql(sql, param);
        return sql.toString();
    }

    /**
     * 查询推荐返利用户列表统计
     * @param param 参数
     * @return
     */
    public String queryRecommendUser(HashMap<String, Object> param) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("SELECT\n" +
                "\tt.* \n" );
        generateSupplyOrderSql(sql, param);
        sql.append(" order by t.bindTime desc ");
        Page page = (Page) param.get("page");
        return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
    }
    private void generateSupplyOrderSql(StringBuffer sql, HashMap<String, Object> param) {
        RecommendUser recommendUser = (RecommendUser) param.get("recommendUser");
        sql.append(" FROM\n" +
                "\t(\n" +
                "\t\tSELECT\n" +
                "\t\t\tsu.user_id userId,\n" +
                "\t\t\tDATE_FORMAT(su.bind_time),'%Y-%m-%d %H:%i:%S') bindTime,\n" +
                "\t\t\tsu.valid_state validState,\n" +
                "\t\t\ti.`name` userName,\n" +
                "\t\t\tsu.recommend_id recommendId,\n" +
                "\t\t\t(\n" +
                "\t\t\t\tSELECT\n" +
                "\t\t\t\t\tinfo. NAME\n" +
                "\t\t\t\tFROM\n" +
                "\t\t\t\t\tyuhuang_sup_info info\n" +
                "\t\t\t\tWHERE\n" +
                "\t\t\t\t\tinfo.user_id = su.recommend_id\n" +
                "\t\t\t) recommendName,\n" +
                "\t\t\t(\n" +
                "\t\t\t\tSELECT\n" +
                "\t\t\t\t\tm.model_name\n" +
                "\t\t\t\tFROM\n" +
                "\t\t\t\t\tyuhuang_supply_model m\n" +
                "\t\t\t\tWHERE\n" +
                "\t\t\t\t\tm.id = su.supply_model\n" +
                "\t\t\t) modelName\n" +
                "\t\tFROM\n" +
                "\t\t\tyuhuang_sup_user su,\n" +
                "\t\t\tyuhuang_sup_info i\n" +
                "\t\tWHERE\n" +
                "\t\t\tsu.user_id = i.user_id\n" +
                "\t\tAND su.recommend_id IS NOT NULL\n" +
                "\t) t ");
        if(StringUtils.isNotBlank(recommendUser.getRecommendName())){
            sql.append(" AND (instr(t.recommendId,#{recommendUser.recommendName})>0 or instr(t.recommendName,#{recommendUser.recommendName})>0)");
        }
        if(StringUtils.isNotBlank(recommendUser.getUserName())){
            sql.append(" AND (instr(t.userId,#{recommendUser.userName})>0 or instr(t.userName,#{recommendUser.userName})>0)");
        }
        if(StringUtils.isNotBlank((String)param.get("startTime"))){
            sql.append(" AND t.bindTime>=#{startTime}");
        }
        if(StringUtils.isNotBlank((String)param.get("endTime"))){
            sql.append(" AND t.bindTime<=#{endTime}");
        }
        if(recommendUser.getValidState()!=null){
            sql.append(" AND t.validState=#{recommendUser.validState}");
        }
    }

}
